/* 
 * File:   CircleBody.cpp
 * Author: aljosa
 * 
 * Created on June 28, 2010, 6:10 PM
 */

#include "CircleBody.h"
#include "../RenderingSystem.h"
#include "../ResourceManager.h"

#include "../MathUtil.h"
#include <cmath>

CircleBody::CircleBody(b2World *world) : Box2DNode(world) {
    texId = -1;
    border = NULL;
    fill = NULL;
}


CircleBody::~CircleBody() {
}

// Draw
void CircleBody::draw(double deltaTime) {

            const float32 k_segments = 16.0f;
            const float32 k_increment = 2.0f * b2_pi / k_segments;
            float32 theta = 0.0f;


            glPushMatrix();

            glTranslatef (WORLD_TO_SCREEN( this->body->GetXForm().position.x), WORLD_TO_SCREEN(this->body->GetXForm().position.y), 0.0f);
            glRotatef ( Rad2Deg( this->body->GetAngle() ), 0.0f, 0.0f, 1.0f );



            glColor3f(1.0f, 1.0f, 1.0f);

            glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
            glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );

            if ( this->texId != -1 ) {

                glEnable(GL_TEXTURE_2D);
                glBindTexture(GL_TEXTURE_2D,  ResourceManager::getInstance().getTexture( this->texName )->texID );

                               glBegin(GL_TRIANGLE_FAN);
                for (int32 i = 0; i < k_segments; ++i)
                {
                        b2Vec2 v = b2Vec2(0.0f, 0.0f) + radious * b2Vec2(cosf(theta), sinf(theta));
                        float len = v.Length();
                        glTexCoord2f((v.x/len - v.x/(len*2))*this->texRepeatX,   (v.y/len  - v.y/(len*2))*this->texRepeatY );
                        glVertex2f(v.x, v.y);
                        theta += k_increment;
                }
                glEnd();


            }
           // xEngine::Renderer::RenderingSystem::get()->Box2DRender.DrawSolidPolygon(((b2PolygonDef*)shapeDef)->vertices, ((b2PolygonDef*)shapeDef)->vertexCount, b2Color(0.0, 0.0, 1.0)  ); //DrawCircle( (b2CircleShape*)shape->GetLocalPosition(),


            else if ( this->fill ) {
                glEnable(GL_BLEND);
                glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

                glColor4f( fill->getRed(), fill->getGreen(), fill->getBlue(), fill->getAlpha() );

                glBegin(GL_TRIANGLE_FAN);
                for (int32 i = 0; i < k_segments; ++i)
                {
                        b2Vec2 v = b2Vec2(0.0f, 0.0f) + radious * b2Vec2(cosf(theta), sinf(theta));
                        glVertex2f(v.x, v.y);
                        theta += k_increment;
                }
                glEnd();

            }





            if ( this->fill ) {
                glColor4f( border->getRed(), border->getGreen(), border->getBlue(), border->getAlpha() );


                glBegin(GL_LINE_LOOP);
                for (int32 i = 0; i < k_segments; ++i)
                {
                        b2Vec2 v = b2Vec2(0.0f, 0.0f) + radious * b2Vec2(cosf(theta), sinf(theta));
                        glVertex2f(v.x, v.y);
                        theta += k_increment;
                }
                glEnd();
              //  const int tris = 20;
             //   unsigned it_val = 360/tris;
        //  glBegin(GL_LINE_LOOP);
            /*    glBegin(GL_TRIANGLE_FAN);
                for (int i=0; i < 360; i+=it_val) //0.017453278 9
                {
                   double degInRad = i*DEG2RAD;
                   glVertex3f(cos(degInRad)*radius,sin(degInRad)*radius, 0.0);
                }
                glEnd(); */
            

            }


             //   xEngine::Renderer::RenderingSystem::get()->Box2DRender.DrawCircle(b2Vec2(0.0f, 0.0f), radious, b2Color(3.0f, 0.0f, 0.0f));
            glPopMatrix();
            glDisable(GL_BLEND);
            glDisable(GL_TEXTURE_2D);

            xEngine::PlatformIndependent::SceneNode::draw(deltaTime);
}

